package com.iot08.api.service;

import com.iot08.api.vo.request.AddVisitHistoryReqVO;
import com.iot08.common.config.mongodb.query.UpdateVO;
import com.iot08.common.entity.app.Account;
import com.iot08.common.entity.app.VisitHistory;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description: 用户信息数据处理
 * @time: 2020/12/21 18:03
 * @author: Rock
 */
@Service
public class VisitHistoryService extends BaseService<VisitHistory> {

    @Resource
    private AccountService accountService;

    /**
     * 用户信息记录 采集
     * @param vo
     * @time: 2021-10-21 18:19
     * @author: Weichao
     * @version 1.0
     */
    public void add(AddVisitHistoryReqVO vo) {
        String openid = vo.getOpenid();
        VisitHistory visitHistory = this.findById(openid);
        if (visitHistory == null) {
            visitHistory = new VisitHistory();
            visitHistory.setId(openid);
            visitHistory.setOpenid(vo.getOpenid());
            visitHistory.setTime(System.currentTimeMillis());
            visitHistory.setRegTime(System.currentTimeMillis());
            Account account = accountService.findById(vo.getOpenid());
            if (account != null) {
                if (account.getDeviceMap() != null && account.getDeviceMap().size() > 0) {
                    visitHistory.setDevices(account.getDeviceMap());
                }
            }
            if (StringUtil.isNotBlank(vo.getLon())) {
                visitHistory.setLon(vo.getLon());
            }
            if (StringUtil.isNotBlank(vo.getLat())) {
                visitHistory.setLat(vo.getLat());
            }
            if (StringUtil.isNotBlank(vo.getProvince())) {
                visitHistory.setProvince(vo.getProvince());
            }
            if (StringUtil.isNotBlank(vo.getCity())) {
                visitHistory.setCity(vo.getCity());
            }
            if (StringUtil.isNotBlank(vo.getAddress())) {
                visitHistory.setAddress(vo.getAddress());
            }
            this.save(visitHistory);
        } else {
            UpdateVO update = new UpdateVO();
            update.set("openid", vo.getOpenid());
            update.set("time", System.currentTimeMillis());
            Account account = accountService.findById(vo.getOpenid());
            if (account != null) {
                update.setMap("d", account.getDeviceMap());
            }
            if (StringUtil.isNotBlank(vo.getLon())) {
                update.set("lon", vo.getLon());
            }
            if (StringUtil.isNotBlank(vo.getLat())) {
                update.set("lat", vo.getLat());
            }
            if (StringUtil.isNotBlank(vo.getProvince())) {
                update.set("province", vo.getProvince());
            }
            if (StringUtil.isNotBlank(vo.getCity())) {
                update.set("city", vo.getCity());
            }
            if (StringUtil.isNotBlank(vo.getAddress())) {
                update.set("address", vo.getAddress());
            }
            this.updateById(openid, update);
        }
    }

}
